﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DelegatePool;



namespace TransformAdd
{
    public static class AddTransformOperation
    {
        public static Transform GetRandomChild(this Transform instance)
        {
            var randomIndex = Random.Range(0, instance.childCount);
            var randomChild = instance.GetChild(randomIndex);

            return randomChild;
        }

        public static float GetDistance(this Transform instance, Transform target)
        {
            var distance = Vector3.Distance(instance.position, target.position);
            return distance;
        }

        public static bool IsTargetDistanceInRange(this Transform instance, Transform target, float distance)
        {
            var abDistance = instance.GetDistance(target);
            return abDistance < distance;
        }

        public static bool IsTargetDistanceInRange(this Transform instance, Vector3 target, float distance)
        {
            var abDistance = Vector3.Distance(instance.position, target);
            return abDistance < distance;
        }

        public static void OperatorEveryChild(this Transform instance, HandleTransform Action)
        {
            for (int i = 0; i < instance.childCount; i++)
            {
                var child = instance.GetChild(i);
                Action(child);
            }
        }

        public static List<Transform> ChildrenToTransformList(this Transform instance)
        {
            List<Transform> result = new List<Transform>();

            for (int i = 0; i < instance.childCount; i++)
            {
                var child = instance.GetChild(i);
                result.Add(child);
            }

            return result;
        }

        public static void SetPostionWithOrigionalVector(this Transform instance, Vector3 position, Vector3 Aixs)
        {
            if (Aixs == Vector3.right)
            {
                position.x = instance.position.x;
                instance.position = position;
            }
            else if (Aixs == Vector3.up)
            {
                position.y = instance.position.y;
                instance.position = position;
            }
            else if (Aixs == Vector3.forward)
            {
                position.z = instance.position.z;
                instance.position = position;
            }
            else
            {
                Debug.LogError("尝试保持某轴不变设置位置时，轴不符合世界坐标系！");
            }
        }
    }
}
